package com.fxjava.displayanrdate.controller;

import com.fxjava.displayanrdate.service.AnrDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 接口控制器（后端 API 网关）
 *
 * 说明：本类下各方法与前端调用一一对应，用于为仪表盘各模块提供数据。
 * - 前端 API 定义：frontend/src/api/anr.js
 * - 使用位置：Dashboard 各组件（KpiCards、TrendCharts、CategoryCharts、SupplierCharts、ModelTimeTrend、ModelProblemStats 等）
 *
 * 注意：当前前端还会请求以下两个接口，但控制器中对应方法已被注释，调用会返回 404：
 * - GET /api/category-detail （CategoryCharts 下钻明细饼图）
 * - GET /api/initial-screening-efficiency（初筛结果与效率直方/箱型图）
 * 这两项对应的 Service/Mapper 已实现，如需启用只需在本 Controller 中恢复映射方法。
 */
@RestController
@RequestMapping("/api")
@CrossOrigin(origins = "*")
public class AnrDataController {

  @Autowired
  private AnrDataService anrDataService;

  /**
   * 前端功能：KPI 统计卡片（本周问题数、本月问题数、Top 型号就是统计前端的第一行要变化的那三个卡片）
   * 对应前端：getKpiData（frontend/src/api/anr.js），组件 KpiCards.vue
   */
  @GetMapping("/kpi")
  public Map<String, Object> getKpiData(@RequestParam(required = false) String timeRange,
                                        @RequestParam(required = false, value = "customDateRange") String[] customDateRange,
                                        @RequestParam(required = false, value = "customDateRange[]") String[] customDateRangeAlt,
                                        @RequestParam(required = false, value = "models") String[] models,
                                        @RequestParam(required = false, value = "models[]") String[] modelsAlt) {
    String[] finalDateRange = (customDateRange != null && customDateRange.length > 0) ? customDateRange : customDateRangeAlt;
    String[] finalModels = (models != null && models.length > 0) ? models : modelsAlt;
    return anrDataService.getKpiData(timeRange, finalDateRange, finalModels);
  }

  /**
   * 前端功能：趋势分析（时间趋势/型号堆叠/热力图）->对应前端的第二和第三部分,这三张图对应一个接口
   * 对应前端：getTrendData（frontend/src/api/anr.js），组件 TrendCharts.vue
   */
  @GetMapping("/trend")
  public Map<String, Object> getTrendData(@RequestParam(required = false) String timeRange,
                                          @RequestParam(required = false, value = "customDateRange") String[] customDateRange,
                                          @RequestParam(required = false, value = "customDateRange[]") String[] customDateRangeAlt,
                                          @RequestParam(required = false, value = "models") String[] models,
                                          @RequestParam(required = false, value = "models[]") String[] modelsAlt) {
    String[] finalDateRange = (customDateRange != null && customDateRange.length > 0) ? customDateRange : customDateRangeAlt;
    String[] finalModels = (models != null && models.length > 0) ? models : modelsAlt;
    return anrDataService.getTrendDataStrict(timeRange, finalDateRange, finalModels);
  }

  /**
   * 前端功能：问题分类与初筛分布（饼图）+ 修复率仪表盘
   * 对应前端：getCategoryData（frontend/src/api/anr.js），组件 CategoryCharts.vue
   */
  @GetMapping("/category")
  public Map<String, Object> getCategoryData(@RequestParam(required = false) String timeRange,
                                             @RequestParam(required = false, value = "customDateRange") String[] customDateRange,
                                             @RequestParam(required = false, value = "customDateRange[]") String[] customDateRangeAlt,
                                             @RequestParam(required = false, value = "models") String[] models,
                                             @RequestParam(required = false, value = "models[]") String[] modelsAlt) {
    String[] finalDateRange = (customDateRange != null && customDateRange.length > 0) ? customDateRange : customDateRangeAlt;
    String[] finalModels = (models != null && models.length > 0) ? models : modelsAlt;
    return anrDataService.getCategoryData(timeRange, finalDateRange, finalModels);
  }

  /**
   * 前端功能：供应商流向（桑基图）与词云占位
   * 对应前端：getSupplierData（frontend/src/api/anr.js），组件 SupplierCharts.vue
   */
  @GetMapping("/supplier")
  public Map<String, Object> getSupplierData(@RequestParam(required = false) String timeRange,
                                             @RequestParam(required = false) String[] customDateRange,
                                             @RequestParam(required = false) String[] models) {
    return anrDataService.getSupplierData(timeRange, customDateRange, models);
  }

  /**
   * 前端功能：筛选栏型号下拉选项
   * 对应前端：getModelList（frontend/src/api/anr.js），组件 FilterBar.vue
   */
  @GetMapping("/models")
  public List<String> getModelList() {
    return anrDataService.getModelList();
  }

  /**
   * 前端功能：时间范围选项（week/month/custom）
   * 对应前端：getTimeRangeOptions（frontend/src/api/anr.js）
   */
  @GetMapping("/time-ranges")
  public Map<String, String> getTimeRangeOptions() {
    return Map.of(
            "week", "本周",
            "month", "本月",
            "custom", "自定义");
  }

  /**
   * 前端功能：型号问题统计 Top5（柱状图+摘要）
   * 对应前端：getModelProblemStats（frontend/src/api/anr.js），组件 ModelProblemStats.vue
   */
  @GetMapping("/model-problem-stats")
  public Map<String, Object> getModelProblemStats(@RequestParam(required = false) String timeRange,
                                                  @RequestParam(required = false, value = "customDateRange") String[] customDateRange,
                                                  @RequestParam(required = false, value = "customDateRange[]") String[] customDateRangeAlt,
                                                  @RequestParam(required = false, value = "models") String[] models,
                                                  @RequestParam(required = false, value = "models[]") String[] modelsAlt) {
    String[] finalDateRange = (customDateRange != null && customDateRange.length > 0) ? customDateRange : customDateRangeAlt;
    String[] finalModels = (models != null && models.length > 0) ? models : modelsAlt;
    return anrDataService.getModelProblemStats(timeRange, finalDateRange, finalModels);
  }

  // 2.3 型号 时间趋势（按月/按周）
  /**
   * 前端功能：型号-时间趋势（折线/堆叠柱，按月或按 ISO 周）
   * 对应前端：getModelTrend（frontend/src/api/anr.js），组件 ModelTimeTrend.vue
   */
  @GetMapping("/model-trend")
  public Map<String, Object> getModelTrend(@RequestParam(required = false) String timeRange,
                                           @RequestParam(required = false) String[] customDateRange,
                                           @RequestParam(required = false) String[] models,
                                           @RequestParam(required = false, defaultValue = "month") String period) {
    return anrDataService.getModelTrend(timeRange, customDateRange, models, period);
  }

  // 3.1 问题分类占比 - 点击扇区下钻 detail_desc
  @GetMapping("/category-detail")
  public Map<String, Object> getCategoryDetail(@RequestParam(required = false) String timeRange,
                                               @RequestParam(required = false) String[] customDateRange,
                                               @RequestParam(required = false) String[] models,
                                               @RequestParam String category) {
    return anrDataService.getCategoryDetailData(timeRange, customDateRange, models, category);
  }
//  // 3.2 修复率分析：饼图 / 折线图 + 仪表盘
//  @GetMapping("/fix-rate-analysis")
//  public Map<String, Object> getFixRateAnalysis(@RequestParam(required = false) String timeRange,
//                                                @RequestParam(required = false) String[] customDateRange,
//                                                @RequestParam(required = false) String[] models,
//                                                @RequestParam(required = false, defaultValue = "month") String period) {
//    return anrDataService.getFixRateAnalysis(timeRange, customDateRange, models, period);
//  }
}
